<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="80px" label-position="left">
			<el-form-item label="排序号">
				<el-input-number v-model="form.sortNo" clearable placeholder="排序号" style="width: 100%"></el-input-number>
			</el-form-item>
			<el-form-item label="规则名称" prop="title">
				<el-input v-model="form.title" clearable placeholder="规则名称" maxlength="40"></el-input>
			</el-form-item>
			<el-form-item label="编号规则" prop="model">
				<el-input v-model="form.model" clearable placeholder="编号规则" maxlength="80"></el-input>
			</el-form-item>
			<el-form-item label="重置规则" prop="resetRule">
				<el-select v-model="form.resetRule" style="width: 100%">
				<el-option v-for="item in resetRuleList" :key="item.value" :label="item.label" :value="item.value"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="起始序号">
				<el-input-number v-model="form.startNo" clearable placeholder="起始序号" style="width: 100%"></el-input-number>
			</el-form-item>
			<el-form-item label="序列长度" prop="numLength">
				<el-input-number v-model="form.numLength" clearable placeholder="序列长度" tyle="width: 100%"></el-input-number>
			</el-form-item>
			<el-form-item label="备注" prop="remark" style="margin-top:10px">
				<el-input v-model="form.remark" maxlength="380" clearable type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增编号规则',
				edit: '编辑编号规则'
			},
			resetRuleList:[
				{value:"0",label:"不重置"},
				{value:"1",label:"按年重置"},
				{value:"2",label:"按月重置"},
				{value:"3",label:"按日重置"},
			],
			visible: false,
			isSaveing: false,
			form: {
				sortNo:'',
				ruleId:"",
				title: "",
				model: "",
				resetRule: "0",
				startNo:0,
				numLength:4,
				remark:""
			},
			rules: {
				title: [
					{required: true, message: '编号规则名称不能为空'}
				],
				model: [
					{required: true, message: '编号规则不能为空'}
				],
				numLength:[
					{required: true, message: '序列长度不能为空'}
				]
			},
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode='add'){
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit(){
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res
					if(this.mode=="add")
					{
						res = await this.$API.system.sysCodeRule.insertSysCodeRule.post(this.form);
					}else
					{
						res = await this.$API.system.sysCodeRule.updateSysCodeRule.post(this.form);
					}
					this.isSaveing = false;
					if(res.code == 200){
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					}else{
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		setData(data){
			this.form.sortNo = data.sortNo
			this.form.ruleId = data.ruleId
			this.form.title = data.title
			this.form.model = data.model
			this.form.resetRule = data.resetRule
			this.form.numLength = data.numLength
			this.form.startNo = data.startNo
			this.form.remark = data.remark
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			//Object.assign(this.form, data)
		}
	}
}
</script>

<style>
</style>
